package com.scisoft.scan.control.delivery;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.scisoft.scan.data.delivery.DeliveryOrderDetailBean;
import com.scisoft.scan.dy.R;
import com.squareup.picasso.Picasso;

import java.util.List;

public class DeliveryOrderDetailAdapter extends BaseAdapter {
    private List<DeliveryOrderDetailBean> deliveryOrderDetailBeanList;//数据源
    private LayoutInflater mInflater;//布局装载器对象
    private Context mContext;

    public DeliveryOrderDetailAdapter(List<DeliveryOrderDetailBean> deliveryOrderDetailBeanList, Context context) {
        this.deliveryOrderDetailBeanList = deliveryOrderDetailBeanList;
        mInflater = LayoutInflater.from(context);
        mContext = context;
    }
    @Override
    public int getCount() {
        //return返回的是int类型，也就是页面要显示的数量。
        return deliveryOrderDetailBeanList.size();
    }
    @Override
    public Object getItem(int position) {
        return deliveryOrderDetailBeanList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //将布局文件转化为View对象
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_deliveryorderdetail,null);
        }

        //订单编号
        TextView tv_orderNo = convertView.findViewById(R.id.tv_orderNo);
        //装箱编号
        TextView tv_packBoxNo = convertView.findViewById(R.id.tv_packBoxNo);
        //配件代码
        TextView tv_partNo = convertView.findViewById(R.id.tv_partNo);
        //配件名称
        TextView tv_partName= convertView.findViewById(R.id.tv_partName);
        //供应商
        TextView tv_supplierName= convertView.findViewById(R.id.tv_supplierName);
        //数量
        TextView tv_count = convertView.findViewById(R.id.tv_count);
        //图片
        ImageView iv_img = convertView.findViewById(R.id.iv_img);
        //获取相应索引的ItemBean对象
        DeliveryOrderDetailBean bean = deliveryOrderDetailBeanList.get(position);

        //设置控件的对应属性值
        tv_orderNo.setText(bean.orderNo);
        tv_packBoxNo.setText(bean.packingNo);
        tv_partNo.setText(bean.partCode);
        tv_partName.setText(bean.partName);
        tv_supplierName.setText(bean.supplierCode + " " + bean.supplierName);
        tv_count.setText(bean.partCount + " 件");
        if (bean.imgUrl != null && !bean.imgUrl.equals("")) {
            Picasso.with(mContext).load(bean.imgUrl).into(iv_img);
        } else {
            iv_img.setImageDrawable(convertView.getResources().getDrawable(R.drawable.ic_noimage));
        }
        return convertView;
    }
}
